package com.css.fxfzfxqh.modules.reportmanagement.repository.impl;

import com.css.fxfzfxqh.modules.lifelineEngineering.entity.LifelineTaskEntity;
import com.css.fxfzfxqh.modules.reportTemplate.entity.Template;
import com.css.fxfzfxqh.modules.reportmanagement.repository.LifelineReportRepository;
import com.css.fxfzfxqh.modules.reportmanagement.vo.LifelineReportVo;
import com.css.fxfzfxqh.util.PlatformObjectUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate;
import org.springframework.stereotype.Repository;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Repository
public class LifelineReportRepositoryImpl implements LifelineReportRepository {
    @Resource
    private JdbcTemplate jdbcTemplate;
    @Resource
    private NamedParameterJdbcTemplate namedParameterJdbcTemplate;

    @Override
    public Map<String, Object> getAll(LifelineReportVo vo) {
        StringBuilder sql = new StringBuilder("select * from fxqh_lifeline_task");
        StringBuilder whereSql = new StringBuilder(" where del_flag = '0'");
        if (PlatformObjectUtils.isNotEmpty(vo.getProvince()))
            whereSql.append(" and province ='" + vo.getProvince() + "'");
        if (PlatformObjectUtils.isNotEmpty(vo.getCity()))
            whereSql.append(" and city ='" + vo.getCity() + "'");
        if (PlatformObjectUtils.isNotEmpty(vo.getCounty()))
            whereSql.append(" and county ='" + vo.getCounty() + "'");
        if (PlatformObjectUtils.isNotEmpty(vo.getAssessCode()))
            whereSql.append(" and code like '%" + vo.getAssessCode() + "%'");
        if (PlatformObjectUtils.isNotEmpty(vo.getAssessName()))
            whereSql.append(" and name like '%" + vo.getAssessName() + "%'");
        if (!vo.getLevel())
            whereSql.append(" and role ='" + vo.getRole() + "'");
        StringBuilder orderSql = new StringBuilder(" order by role asc,create_time desc");
        int firstIndex = (vo.getCurPage() - 1) * vo.getPageSize();
        int lastIndex = vo.getPageSize();
        StringBuilder limitSql = new StringBuilder(" limit " + lastIndex + " offset " + firstIndex);
        sql.append(whereSql).append(orderSql);
        Integer total = jdbcTemplate.query(sql.toString(), new BeanPropertyRowMapper<>(LifelineTaskEntity.class)).size();
        List<LifelineTaskEntity> entities = jdbcTemplate.query(sql.append(limitSql).toString(), new BeanPropertyRowMapper<>(LifelineTaskEntity.class));
//        for (LifelineTaskEntity entity : entities) {
//            if ("0".equals(vo.getRole())) {
//                entity.setProvince(null);
//                entity.setCity(null);
//                entity.setCounty(null);
//            }
//            if ("1".equals(vo.getRole())) {
//                entity.setCity(null);
//                entity.setCounty(null);
//            }
//            if ("2".equals(vo.getRole())) {
//                entity.setCounty(null);
//            }
//        }
        Map<String, Object> map = new HashMap();
        map.put("total", total);
        map.put("rows", entities);
        return map;
    }

    @Override
    public Template getTemplate(String reportType, String input) {
        String sql = "select * from fxqh_report_template where report_type = '" + reportType + "' and input = '" + input + "'";
        List<Template> templates = jdbcTemplate.query(sql, new BeanPropertyRowMapper<>(Template.class));
        return templates.get(0);
    }

    @Override
    public void saveReport(LifelineTaskEntity entity) {
        String sql = "update fxqh_lifeline_task set create_report =?,create_report_time =?,create_report_user=?,create_report_name=? where id =? ";
        jdbcTemplate.update(sql, entity.getCreateReport(), entity.getCreateReportTime(), entity.getCreateReportUser(),entity.getCreateReportName(), entity.getId());
    }

    @Override
    public void saveUploadReport(LifelineTaskEntity entity) {
        String sql = "update fxqh_lifeline_task set upload_report=?,upload_report_name=? where id =? ";
        jdbcTemplate.update(sql, entity.getUploadReport(),entity.getUploadReportName(), entity.getId());
    }

}
